package com.yunzheng.studyLeaveSystem.modules.admin.search.trueSearch.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yunzheng.studyLeaveSystem.modules.admin.search.trueSearch.service.SearchService;
import com.yunzheng.studyLeaveSystem.modules.admin.search.trueSearch.entity.SearchEntity;
import com.yunzheng.studyLeaveSystem.modules.admin.search.trueSearch.service.SearchService;
import com.yunzheng.studyLeaveSystem.modules.response.ResponseWrapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: YunZheng
 * @Date: 2023/01/30/10:53
 * @Description:
 */
@WebServlet(value="/SearchServlet")
public class SearchServlet extends HttpServlet {


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //获取请求实体数据
        BufferedReader reader = request.getReader();
        String readLine = reader.readLine();
        //将json字符转化为java对象
        JSONObject jsonObject = JSON.parseObject(readLine);
        String name = jsonObject.getString("name");

        //  name = request.getParameter("name");
        if (null == name) {
            name = "";
        }
        SearchService searchService = new SearchService();
        List<SearchEntity> InformationList;
        try {
            //InformationList = searchService.showInformation(name);
            InformationList = searchService.showInformation(name);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        ResponseWrapper responseWrapper = ResponseWrapper.Success(InformationList);
        String s = JSON.toJSONString(responseWrapper);
        PrintWriter writer = response.getWriter();
        writer.println(s);
        System.out.println(s);
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}
